using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using Microsoft.VisualStudio.Modeling.Design;
using Microsoft.VisualStudio.Modeling;

namespace TVDesigner
{

    public class PropertyColorComboEditor : UITypeEditor
    {
        private IWindowsFormsEditorService _wfes;

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            if (context == null)
            {
                return base.GetEditStyle(context);
            }
            return UITypeEditorEditStyle.DropDown;
        }

        protected void Combo_Click(object pSender, EventArgs pArgs)
        {
            if (_wfes != null)
            {
                _wfes.CloseDropDown();
            }
        }

        public override object EditValue(
            ITypeDescriptorContext context,
            IServiceProvider provider,
            object value)
        {
            if ((context == null) || (provider == null) || (context.PropertyDescriptor == null))
            {
                return base.EditValue(context, provider, value);
            }

            _wfes = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            ColorComboBox combo = new ColorComboBox();
            combo.Click += new EventHandler(Combo_Click);


            if (context.PropertyDescriptor.DisplayName.Equals("Red Button"))
            {
                combo.Color = Color.Red;
            }
            else if (context.PropertyDescriptor.DisplayName.Equals("Green Button"))
            {
                combo.Color = Color.Green;
            }
            else if (context.PropertyDescriptor.DisplayName.Equals("Yellow Button"))
            {
                combo.Color = Color.Yellow;
            }
            else if (context.PropertyDescriptor.DisplayName.Equals("Blue Button"))
            {
                combo.Color = Color.Blue;
            }

            HScreenShape desc = context.Instance as HScreenShape;
            HScreen currentElement = desc.ModelElement as HScreen;

            foreach (HScreen screen in currentElement.Xlet.HScreens)
            {
                if (!screen.Name.Equals(currentElement.Name))
                {
                    combo.Add(screen.Name);
                }
            }

            if (value != null)
            {
                combo.SelectedItem = value;
            }

            _wfes.DropDownControl(combo);

            return combo.SelectedItem;
        }

        private DialogResult ShowForm(IServiceProvider provider, Form form)
        {
            IUIService service = (IUIService)provider.GetService(typeof(IUIService));
            if (service != null)
            {
                return service.ShowDialog(form);
            }
            return form.ShowDialog();
        }
    }
}
